package com.hph.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hph.health.common.BaseResponse;
import com.hph.health.common.ErrorCode;
import com.hph.health.common.ResultUtils;
import com.hph.health.exception.BusinessException;
import com.hph.health.exception.ThrowUtils;
import com.hph.health.model.dto.order.AddOrderRequest;
import com.hph.health.model.dto.order.MyOrderList;
import com.hph.health.model.dto.order.OrderList;
import com.hph.health.model.dto.order.UpdateOrderRequest;
import com.hph.health.model.dto.user.DeleteRequest;
import com.hph.health.model.dto.user.UserUpdateRequest;
import com.hph.health.model.entity.Order;
import com.hph.health.model.entity.User;
import com.hph.health.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hanph
 * 订单接口
 * @from haut
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Resource
    private OrderService orderService;


    /**
     * 获取我的订单列表
     *
     * @param myOrderList
     * @return
     */
    @PostMapping("/mylist")
    public BaseResponse<List<Order>> listmyOrderByPage(@RequestBody MyOrderList myOrderList) {
        String userId = myOrderList.getUserId();
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(userId),"user_id", userId);
        List<Order> list = orderService.list(queryWrapper);
        return ResultUtils.success(list);
    }

    /**
     * 获取商家的订单列表
     *
     * @param orderList
     * @return
     */
    @PostMapping("/merlist")
    public BaseResponse<List<Order>> listMerchantOrderByPage(@RequestBody OrderList orderList) {
        String merchantId = orderList.getMerchantId();
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(merchantId),"merchant_id", merchantId);
        List<Order> list = orderService.list(queryWrapper);
        return ResultUtils.success(list);
    }

    /**
     *  新增订单
     *
     * @param addOrderRequest
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addProduct(@RequestBody AddOrderRequest addOrderRequest) {
        if (addOrderRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Order order = new Order();
        BeanUtils.copyProperties(addOrderRequest, order);
        boolean result = orderService.save(order);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(order.getId());
    }

    /**
     * 更新订单
     *
     * @param updateOrderRequest
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Order> updateUser(@RequestBody UpdateOrderRequest updateOrderRequest) {
        if (updateOrderRequest == null || updateOrderRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Order order = new Order();
        BeanUtils.copyProperties(updateOrderRequest, order);
        boolean result = orderService.updateById(order);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(order);
    }
    /**
     * 删除订单
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(orderService.removeById(deleteRequest.getId()));
    }
}
